package com.itheima.search.controller;

import com.itheima.search.domain.NewsVO;
import com.itheima.search.domain.SearchParam;
import com.itheima.search.domain.SearchResult;
import com.itheima.search.service.NewsSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Controller
public class NewsSearchController {
    @Autowired
    private NewsSearchService newsSearchService;

    /**
     * 根据搜索关键字分页搜索ES中的新闻
     * @param searchParam
     * @return
     */
    @PostMapping("/search")
    @ResponseBody // 添加这个注解来直接返回数据而不是视图
    public SearchResult newsSearch(@RequestBody SearchParam searchParam) throws IOException {
        return newsSearchService.newsSearch(searchParam);
    }


    /**
     * 根据新闻ID搜索ES中的新闻
     * @param newsId
     * @return
     */
    @GetMapping("/search/{newsId}")
    @ResponseBody // 添加这个注解来直接返回数据而不是视图
    public NewsVO queryNewsByid(@PathVariable("newsId")Long newsId) throws IOException {
        return newsSearchService.queryNewsById(newsId);
    }
}
